package com.kdfly.zmdfsj;

import android.content.Context;

import com.kdfly.zmdfsj.model.Constants;
import com.kdfly.zmdfsj.model.Player;
import com.kdfly.zmdfsj.model.Room;
import com.kdfly.zmdfsj.model.Store;

import de.greenrobot.event.EventBus;

/**
 * Created by Sean on 2015-09-08.
 */
public class GameEngine {
    protected static GameEngine instance = new GameEngine();

    protected Store store = new Store();
    protected Player player = new Player();
    protected Room room = new Room();
    protected int[] price = null;
    protected int day = 0;
    protected Context mContext = null;

    protected GameEngine(){}

    public static GameEngine getInstance(){
        return instance;
    }
    public void init(Context context){
        price = store.getPrices(Constants.randomGoodsAmount);
        player.init();
        room.init();

        if (null == mContext)
            mContext = context;
    }

    public Player getPlayer(){
        return player;
    }
    public Room getRoom(){
        return room;
    }
    public int[] getPrice() {
        return price;
    }
    public int getDay(){return day;}

    public void oneDayPass(){
        price = store.getPrices(Constants.randomGoodsAmount);
        day = day + 1;
        if (day > 30){
            EventBus.getDefault().post(Constants.UPDATE_GAME_OVER);
        }

        int Probability = Constants.getRandom(300);
        MarketEvent m = null;
        if (Probability <= 50){
            m = Constants.EVENTS_MARKET_One[Constants.getRandom(Constants.EVENTS_MARKET_One.length-1)];
        }if (Probability > 50 && Probability<= 90){
            m = Constants.EVENTS_MARKET_Two[Constants.getRandom(Constants.EVENTS_MARKET_Two.length-1)];
        }if (Probability > 90 && Probability<= 120){
            m = Constants.EVENTS_MARKET_Three[Constants.EVENTS_MARKET_Three.length-1];
        }if (Probability > 120 && Probability<= 140){
            m = Constants.EVENTS_MARKET_Four[Constants.getRandom(Constants.EVENTS_MARKET_Four.length-1)];
        }if (Probability > 140 && Probability<= 150){
            m = Constants.EVENTS_MARKET_Five[Constants.getRandom(Constants.EVENTS_MARKET_Five.length-1)];
        }if (Probability > 150){
            m = Constants.EVENTS_MARKET_Six[0];
        }
//        System.out.println("测试低于0的商品" + price[m.getGoodId()]);
        boolean isNoRoom = false;
        if (8 == m.getGoodId()){}
        else {
            if (0 == price[m.getGoodId()]) {
//            continue;

            } else {
                if (m.getMultiplier() > 0)
                    price[m.getGoodId()] *= m.getMultiplier();
                else if (m.getDivisor() > 0)
                    price[m.getGoodId()] /= m.getDivisor();
                else if (m.getAddCount() > 0) {
                    int spaceLeft = room.getSpace() - room.getAllGoodsCount();
                    if (spaceLeft >= m.getAddCount())
                        room.addGood(m.getGoodId(), m.getAddCount(), 0);
                    else if (spaceLeft <= 0)
                        isNoRoom = true;
                    else {
                        room.addGood(m.getGoodId(), spaceLeft, 0);
                        isNoRoom = true;
                    }
                }
                EventBus.getDefault().post(mContext.getString(m.getMessageId()));
            }
        }

        calculateFinance();

//        if (m.getAddDebt() > 0) {
//            man.setDebt(man.getDebt() + m.addDebt);
//            EventBus.getDefault().post(Constants.UPDATE_MONEY);
//        }

//        if (isNoRoom)
//            EventBus.getDefault().post(String.format(mContext.getString(R.string.notify_no_room), room.getSpace()));

    }

    public void startNewGame(){
        EventBus.getDefault().post(Constants.START_NEW_GAME);
    }

    protected void calculateFinance(){
        int DebtInc = player.getDebt() * Constants.FIXED_DEBT_PERCENTAGE / 100;
        player.setDebt(player.getDebt() + DebtInc);
    }

    public void goToDay(int a){
        day = a;
        if (day > 30){
            EventBus.getDefault().post(Constants.UPDATE_GAME_OVER);
        }
    }

    public boolean checkCanSell(int goodID){
        if (price[goodID] >0 ){
            return true;
        }else {
            return false;
        }
    }

    public void sellGood(int goodId, int count){
        room.delGood(goodId, count);
        player.setMoney(player.getMoney() + price[goodId] * count);
        EventBus.getDefault().post(Constants.UPDATE_MONEY);
        EventBus.getDefault().post(Constants.UPDATE_ROOM);
    }

    public void buyGood(int goodId,int count){
        if (count<=0)
            return;

        int roomSpace = room.getSpace() - room.getAllGoodsCount();
        int money = player.getMoney() - (price[goodId] * count);

        if ((money < 0)||(roomSpace < count))
            return;

        player.setMoney(money);
        room.addGood(goodId, count, price[goodId]);
        EventBus.getDefault().post(Constants.UPDATE_MONEY);
        EventBus.getDefault().post(Constants.UPDATE_ROOM);
    }
}
